using System;
using System.Collections.Generic;
using System.Linq;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using System.ComponentModel.DataAnnotations;
using HengTong.Model.Db.System;


namespace HengTong.ViewModel._Admin.SysNotificationVms
{
    public partial class SysNotificationListVm : BasePagedListVM<SysNotificationView, SysNotificationSearcher>
    {

        protected override IEnumerable<IGridColumn<SysNotificationView>> InitGridHeader()
        {
            return new List<GridColumn<SysNotificationView>>{
                this.MakeGridHeader(x => x.Title),
                this.MakeGridHeader(x => x.Content),
                this.MakeGridHeader(x => x.RouteName),
                this.MakeGridHeader(x => x.CategoryName),
                this.MakeGridHeader(x => x.BusinessType),
                this.MakeGridHeader(x => x.BusinessId),
                this.MakeGridHeader(x => x.Sender),
                this.MakeGridHeader(x => x.Receiver),
                this.MakeGridHeader(x => x.Status),
                this.MakeGridHeader(x => x.Priority),
                this.MakeGridHeader(x => x.IsTop),
                this.MakeGridHeader(x => x.SendTime),
                this.MakeGridHeader(x => x.ReadTime),
                this.MakeGridHeaderAction(width: 200)
            };
        }
        
        public override IOrderedQueryable<SysNotificationView> GetSearchQuery()
        {
            if (Wtm.LoginUserInfo.Roles.All(e => e.RoleCode != "001"))
            {
                // 如果不是超级管理员，则只能查看自己的通知
                Searcher.Receiver = Wtm.LoginUserInfo.ITCode;
            }
            var query = DC.Set<SysNotification>()
                .CheckEqual(Searcher.CategoryId, x=>x.CategoryId)
                .CheckEqual(Searcher.Status, x=>x.Status)
                .CheckEqual(Searcher.Priority, x=>x.Priority)
                .CheckEqual(Searcher.Sender, x => x.Sender)
                .CheckEqual(Searcher.Receiver, x => x.Receiver)
                .Select(x => new SysNotificationView
                {
				    ID = x.ID,
                    Title = x.Title,
                    Content = x.Content,
                    RouteName = x.Route.Name,
                    CategoryName = x.Category.Name,
                    BusinessType = x.BusinessType,
                    BusinessId = x.BusinessId,
                    Sender = x.Sender,
                    Receiver = x.Receiver,
                    Status = x.Status,
                    Priority = x.Priority,
                    IsTop = x.IsTop,
                    SendTime = x.SendTime,
                    ReadTime = x.ReadTime,
                    CategoryIcon = x.Category.Icon
                })
                .OrderByDescending(x => x.SendTime);
            return query;
        }

    }

    public class SysNotificationView : SysNotification{
        [Display(Name = "单据名称")]
        public String RouteName { get; set; }
        
        [Display(Name = "分类名称")]
        public String CategoryName { get; set; }
        
        [Display(Name = "分类图标")]
        public string CategoryIcon { get; set; }
    }
}
